<template>
  <div class="role">
    <!-- 按钮 -->
    <div class="btns">
      <el-form :inline="true" size="small" >
        <el-form-item>
      <el-button type="primary" size="small" @click="toAdd">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- /按钮 -->
    <!-- 内容 -->
    <el-table size="small" :data="roles">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="角色" prop="name" align="center"></el-table-column>
      <el-table-column label="操作" width="160" align="center">
        <template v-slot:default="scope">
          <el-button type="text" size="mini" @click="editRole(scope.row)" icon="el-icon-edit" circle></el-button>
          <el-button type="text" size="mini" @click="delRole(scope.row)" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /内容 -->
    <!-- 分页 -->
    <center>
     <el-pagination
      v-if="roles.total > param.pageSize"
      layout="prev, pager, next"
      @current-change="currentChangeHandler"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="roles.total">
    </el-pagination>
    </center>
    <!-- /分页 -->
    <!-- 模态框 -->
    <el-dialog title="角色" :visible.sync="visible" width="60%">
      <el-form label-width="80px" size="small">
        <el-form-item label="角色名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>

<script>
// 1. 导入请求函数
import {get,post } from '@/utils/request'
export default {
  data(){
    return {
      visible:false,
      param:{
        page:1,
        pageSize:10
      },
      roles:{},
      form:{
        gender:"女"
      }
      
    }
  },
  // 2.在vue实例创建完毕之后就调用加载用户信息的接口
  created(){
    this.loadRoleData();
  },
  methods:{
    // 修改用户信息
    editRole(row){
      // 将要修改的信息赋值给当前表单，进行双向数据绑定，再次基础上完成修改操作
      this.form = row; 
      this.visible = true;
    },
    // 删除用户信息
    delRole(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/role/deleteById";
        get(url,{id:row.id}).then(response => {
          this.$message({type:"success",message:response.message});
          this.loadRoleData();
        })
      })
    },
    // 提交表单
    submit(){
      // 1. 获取表单数据  this.form
      // 2. 交互
      let url = "/role/saveOrUpdate"
      post(url,this.form).then(response => {
        // 3. 提示
        this.$message({type:"success",message:response.message});
        // 4. 关闭模态框
        this.visible = false;
        // 5. 刷新数据
        this.loadRoleData();
      })
     
    },
    // 加载角色信息
    loadRoleData(){
      let url ="/role/findAll";
      get(url,this.param).then(response => {
        this.roles = response.data;
      })
    },
    // 打开模态框去添加，
    toAdd(){
      this.form = {gender:"女"}
      this.visible = true;
    },
    // 翻页
    currentChangeHandler(page){
      this.param.page = page;
      this.loadRoleData();
    
    }
  }
}
</script>
